//
//  ButtonView.m
//  International_Hospital
//
//  Created by 喵小仲 on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "ButtonView.h"

@interface ButtonView()

//确定string
@property (nonatomic, copy) NSString *yesString;

//取消string
@property (nonatomic, copy) NSString *noString;

@end

@implementation ButtonView

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //背景颜色
    self.backgroundColor = [UIColor whiteColor];
    
    self.layer.cornerRadius = kLLCornerRadius;
}

#pragma mark
#pragma mark -- 设置makeSureView的子控件
- (void)setupUIWithPlaceHolder:(NSString *)string yesString:(NSString *)yesString noString:(NSString *)noString
{
    self.yesString = yesString;
    self.noString = noString;
    self.placeHolderString = string;
    
    //输入框
    UITextField *textField = [[UITextField alloc] init];
    self.textField = textField;
    textField.placeholder = string;
    textField.textColor = [UIColor blackColor];
    textField.enabled = NO;
    [self addSubview:textField];
    
    //确定按钮
    UIButton *yesButton = [[UIButton alloc] init];
    self.yesButton = yesButton;
    [yesButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [yesButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [yesButton setTitle:yesString forState:UIControlStateNormal];
    [yesButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [yesButton addTarget:self action:@selector(didClickYesButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:yesButton];
    
    //取消按钮
    UIButton *noButton = [[UIButton alloc] init];
    self.noButton = noButton;
    [noButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [noButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [noButton setTitle:noString forState:UIControlStateNormal];
    [noButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [noButton addTarget:self action:@selector(didClickNoButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:noButton];
    
    //各种约束
    [self.textField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(0);
        make.left.offset(kLLMarginLeft);
    }];
    
    [self.yesButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.centerX.offset(kLLMarginLeft);
    }];
    
    [self.noButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(yesButton.right).offset(kLLMarginLeft);
        make.centerY.equalTo(0);
    }];
    
    self.textField.font = kLLFontSize;
    self.yesButton.titleLabel.font = kLLFontSize;
    self.noButton.titleLabel.font = kLLFontSize;
}

#pragma mark
#pragma mark -- 点击确认按钮的方法
- (void)didClickYesButton:(UIButton *)button
{
    if (button.isSelected)
    {
        button.selected = NO;
        
        self.textField.text = @"";
        
        if (self.cureTypeViewBlock)
        {
            self.cureTypeViewBlock();
        }
    }
    else
    {
        button.selected = YES;
        
        self.noButton.selected = NO;
        self.textField.text = self.yesString;
        
        if (self.cureTypeViewBlock)
        {
            self.cureTypeViewBlock();
        }
    }
}

#pragma mark
#pragma mark -- 点击取消按钮的方法
- (void)didClickNoButton:(UIButton *)button
{
    if (button.isSelected)
    {
        button.selected = NO;
        
        self.textField.text = @"";
    }
    else
    {
        button.selected = YES;
        
        self.yesButton.selected = NO;
        self.textField.text = self.noString;
        
        if (self.cureTypeViewBlock)
        {
            self.cureTypeViewBlock();
        }
    }
}

@end
